package me.macd.mindoc.service.impl;

import me.macd.mindoc.dao.MemberDao;
import me.macd.mindoc.model.MemberRelationshipResult;
import me.macd.mindoc.service.MemberRelationshipService;
import me.macd.mindoc.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @program: mindoc
 * @description: ${description}
 * @author: 2018-10-02
 * @create: 2018-10-02 09:53
 */
@Service
public class MemberRelationshipServiceImpl implements MemberRelationshipService {

    @Autowired
    MemberDao memberDao;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public Page<MemberRelationshipResult> findForUsersByBookId(int bookId, int pageIndex, int pageSize) {
        Page<MemberRelationshipResult> page = new Page<>();

        int offset = (pageIndex - 1) * pageSize;

        int count = memberDao.countForUsersByBookId(bookId);
        List<MemberRelationshipResult> list = memberDao.findForUsersByBookId(bookId, offset, pageSize);

        page.setItems(list);
        page.setTotalCount(count);

        return page;
    }
}
